<?php
/**
* Author	: TRUONG KIM PHUNG
* Class		: Share
* Description
	* Manage Share
	* Get
	* Add
	* Update
	* Delete
* Contact	:
	- Email : truongkimphung1982@gmail.com
	- Y!	: truongkimphung1982
	- Skype	: truongkimphung1982
**/
class Share extends Ecore_Db {
	var $cache;
	var $clear;
	public function __construct() {
		$this->cache = new Ecore_Cache();
		$this->clear = $this->cache->clear;
	}	
	/**
	 * GET INFORMATION SHARE BY ID
	 * @PARAM
			- $intShareId
	 * @RESULT
			- Array | NULL
	 */
	public function get($intShareId) {
		try {
			$strKey = SHARE_GET . $intShareId;
			$arrResult	= $this->cache->get($strKey);
			if(!Ecore_Function::isCached($arrResult)) {
				$this->prepare('call shares_get(?)');
				$this->bindParam(1 , $intShareId	, PDO::PARAM_INT, 11);			
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, SHARE_GET_EXPIRE);
			}
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Share::get - ' . $e->getMessage());
		}
	}
	/**
	 * GET TOP BOARD
	 * @PARAM
			- $intType {1:postdate desc | 2:like desc}
			- $intLimit
	 * @RESULT
			- Array | NULL
	 */
	public function getTop($intType,$intLimit=0) {
		try {
			
			$this->prepare('call shares_top(?,?)');
			$this->bindParam(1 , $intType	, PDO::PARAM_INT, 11);				
			$this->bindParam(2 , $intLimit	, PDO::PARAM_INT, 6);				
			$arrResult 	= $this->fetchAll();			
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Share::getTopBoard - ' . $e->getMessage());
		}
	}
	
	/**
	 * GET MINE'S BOARD
	 * @PARAM
			- $intType: {1:mine; 2:hot; 3:like}
			- $intUserId
			- $intOffset
			- $intLimit
			- $intTotal
	 * @RESULT
			- Array | NULL
	 */
	public function getAllBoardByType($intType, $intUserId, $intOffset, $intLimit, &$intTotal=1) {
		try {
			$strKey		= '';
			$arrResult	= null;
			if($intOffset == 0) {
				if($intType > 1) {					
					$strKey = SHARE_GETBOARDBYTYPE . $intUserId . $intLimit;
				} else {
					$strKey	= SHARE_GETBOARDBYTYPE . $intType;
				}
				$arrResult	= $this->cache->get($strKey);
			}

			if(!Ecore_Function::isCached($arrResult)) {
				$this->prepare('call shares_getall_stats_ddht(?,?,?,?)');
				$this->bindParam(1 , $intType		, PDO::PARAM_INT, 4);			
				$this->bindParam(2 , $intUserId		, PDO::PARAM_INT, 11);			
				$this->bindParam(3 , $intOffset		, PDO::PARAM_INT, 11);			
				$this->bindParam(4 , $intLimit		, PDO::PARAM_INT, 6);			
				$arrResult 	= $this->fetchAll();
				if($intOffset == 0)
					$this->cache->set($strKey, $arrResult, SHARE_GETBOARDBYTYPE_EXPIRE);
			}
			$intTotal = isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;			
		} catch(Exception $e) {
			Ecore_Function::logDb('Share::getAllBoardByType - ' . $e->getMessage());
		}
	}
	/**
	 * GET ALL BOARD 
	 * @PARAM			
			- $intCategoryId
			- $intSubId
			- $intOffset
			- $intLimit
			- $intTotal
	 * @RESULT
			- Array | NULL
	 */
	public function getAll($intType, $intCategoryId, $intSubId, $intOffset, $intLimit, &$intTotal=0) {
		try {
			$arrResult	= null;
			$strKey		= '';
			if($intOffset == 0) {
				$strKey 	= SHARE_GETALL . $intType . $intCategoryId . $intSubId;
				$arrResult	= $this->cache->get($strKey);
			}
			if(!Ecore_Function::isCached($arrResult)) {
				$this->prepare('call shares_getall_ddht(?,?,?,?,?)');			
				$this->bindParam(1 , $intType		, PDO::PARAM_INT, 4);			
				$this->bindParam(2 , $intCategoryId	, PDO::PARAM_INT, 11);			
				$this->bindParam(3 , $intSubId		, PDO::PARAM_INT, 11);			
				$this->bindParam(4 , $intOffset		, PDO::PARAM_INT, 11);			
				$this->bindParam(5 , $intLimit		, PDO::PARAM_INT, 4);			
				$arrResult 	= $this->fetchAll();
				if($intOffset == 0) $this->cache->set($strKey, $arrResult, SHARE_GETALL_EXPIRE);
			}
			$intTotal = isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			if($intTotal > 0) {
				$intCo 			= count($arrResult);
				$objSHARE	= new SHARE();
				for($i=0;$i<$intCo;$i++) {
					$strRefer = $arrResult[$i]['refers'];
					if($strRefer != '') {
						if((int)$strRefer > 0) {
							if($arrResult[$i]['type'] < 3 && $arrResult[$i]['type'] > 0) {
								$arrResult[$i]['refers'] = $this->get((int)$strRefer);
							}elseif($arrResult[$i]['type'] > 2 && $arrResult[$i]['type'] < 5) {
								$arrResult[$i]['refers'] = $objSHARE->get((int)$strRefer);
							}
						} else {
							$arrResult[$i]['refers'] = Zend_Json::decode($strRefer);
						}
					}
				}
			}
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Share::getAll - ' . $e->getMessage());
		}
	}
	/**
	 * GET ALL PIN BY BOARDID 
	 * @PARAM			
			- $intBoardId
			- $intType
			- $intOffset
			- $intLimit
			- $intTotal
	 * @RESULT
			- Array | NULL
	 */
	public function getAllPin($intBoardId, $intType, $intOffset, $intLimit, &$intTotal=0) {
		try {
			$arrResult	= null;
			$strKey		= '';
			if($intOffset == 0) {
				$strKey 	= SHARE_GETALLPIN . $intBoardId . $intType;
				$arrResult	= $this->cache->get($strKey);
			}
			if(!Ecore_Function::isCached($arrResult)) {
				$this->prepare('call shares_getall_pin_ddht(?,?,?,?)');			
				$this->bindParam(1 , $intBoardId	, PDO::PARAM_INT, 11);			
				$this->bindParam(2 , $intType		, PDO::PARAM_INT, 11);			
				$this->bindParam(3 , $intOffset		, PDO::PARAM_INT, 11);			
				$this->bindParam(4 , $intLimit		, PDO::PARAM_INT, 4);			
				$arrResult 	= $this->fetchAll();
				if($intOffset == 0) $this->cache->set($strKey, $arrResult, SHARE_GETALLPIN_EXPIRE);
			}
			$intTotal = isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			if($intTotal > 0) {
				$intCo 			= count($arrResult);
				$objSHARE	= new SHARE();
				for($i=0;$i<$intCo;$i++) {
					$strRefer = $arrResult[$i]['refers'];
					if($strRefer != '') {
						if((int)$strRefer > 0) {
							if($arrResult[$i]['type'] < 3 && $arrResult[$i]['type'] > 0) {
								$arrResult[$i]['refers'] = $this->get((int)$strRefer);
							}elseif($arrResult[$i]['type'] > 2 && $arrResult[$i]['type'] < 5) {
								$arrResult[$i]['refers'] = $objSHARE->get((int)$strRefer);
							}
						} else {
							$arrResult[$i]['refers'] = Zend_Json::decode($strRefer);
						}
					}
				}
			}
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Share::getAllPin - ' . $e->getMessage());
		}
	}
	/**
	 * GET ALL RELAVANCE
	 * @PARAM			
			- $intShareId
			- $strTitle
			- $intLimit
	 * @RESULT
			- Array | NULL
	 */
	public function getAllRelavance($intShareId, $strTitle, $intLimit=RELEVANCE_LIMIT) {
		try {
			
			$strKey 	= SHARE_GETALLRELAVANCE . $intShareId;
			$arrResult	= $this->cache->get($strKey);			
			if(!Ecore_Function::isCached($arrResult)) {
				$this->prepare('call shares_getall_relavance(?,?,?)');			
				$this->bindParam(1 , $intShareId	, PDO::PARAM_INT, 11);			
				$this->bindParam(2 , $strTitle		, PDO::PARAM_INT, 11);			
				$this->bindParam(3 , $intLimit		, PDO::PARAM_INT, 4);			
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, SHARE_GETALLRELAVANCE_EXPIRE);
			}			
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Share::getAllRelavance - ' . $e->getMessage());
		}
	}
	/**
	 * GET ALL TOP CMT
	 * @PARAM			
			- $intType {1:lastcmt;2:cmt}
			- $intLimit
	 * @RESULT
			- Array | NULL
	 */
	public function getAllTopCmt($intType,$intLimit=CLIENT_LIMIT) {
		try {		
			$strKey 	= SHARE_GETALLTOPCMT . $intType;
			$arrResult	= $this->cache->get($strKey);			
			if(!Ecore_Function::isCached($arrResult)) {
				$this->prepare('call shares_getall_cmt(?,?)');			
				$this->bindParam(1 , $intType	, PDO::PARAM_INT, 11);			
				$this->bindParam(2 , $intLimit	, PDO::PARAM_INT, 6);			
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, SHARE_GETALLTOPCMT_EXPIRE);
			}			
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Share::getAllTopCmt - ' . $e->getMessage());
		}
	}
	/**
	 * ADD 
	 * @PARAM
			- $intUserId
			- $strTitle
			- $intType
			- $intCategoryId
			- $intSubId
			- $strShort
			- $strContent
			- $strImage
			- $strLink
			- $intParentId
			- $strPrefer
			- $intShareId : Assign last id insert
			- $intAdminId
	 * @RESULT errCode {
				0: success
				1: not perm
			}
	 */
	public function add($intUserId, $strTitle, $intType, $intCategoryId, $intSubId,
						$strShort, $strContent, $strImage, $strLink='', $intParentId=0,
						$strRefer='', &$intShareId=0, $intAdminId=0) {
		try {
			$this->prepare('call shares_add(?,?,?,?,?,?,?,?,?,?,?,?)');
			$this->bindParam(1 , $intAdminId	, PDO::PARAM_INT, 11);			
			$this->bindParam(2 , $intUserId		, PDO::PARAM_INT, 11);			
			$this->bindParam(3 , $strTitle		, PDO::PARAM_STR, 255);			
			$this->bindParam(4 , $intType		, PDO::PARAM_INT, 4);			
			$this->bindParam(5 , $intCategoryId	, PDO::PARAM_INT, 11);			
			$this->bindParam(6 , $intSubId		, PDO::PARAM_INT, 11);			
			$this->bindParam(7 , $strShort		, PDO::PARAM_STR, 255);			
			$this->bindParam(8 , $strContent	, PDO::PARAM_STR);			
			$this->bindParam(9 , $strImage		, PDO::PARAM_STR, 150);			
			$this->bindParam(10 , $strLink		, PDO::PARAM_STR, 255);			
			$this->bindParam(11 , $intParentId	, PDO::PARAM_INT, 11);			
			$this->bindParam(12 , $strRefer		, PDO::PARAM_STR);					
			$arrResult 	= $this->fetchAll();	
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : -1;
			if($intError == 0) {				
				$intShareId	= isset($arrResult[0]['shareid']) ? $arrResult[0]['shareid'] : 0;
				$this->addShareU($intUserId, $intShareId, 0);				
				$this->clear->deleteBoard($intCategoryId, $intSubId, $intUserId);
				$objSearch		= new Ecore_Search();
				$strSlug		= Ecore_Rewrite_Ddht::slugShare($intShareId,$strTitle);
				$objSearch->addUpdate($intShareId, 2, $strTitle, '', '',
									  $strShort, $strImage, $strSlug, 0, 0,
									  Ecore_Function::now(), 1, 2, $intCategoryId, $intSubId);
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Share::add - ' . $e->getMessage());
		}
	}
	/**
	 * UPDATE 
	 * @PARAM
			- $intUserId
			- $intShareId
			- $strTitle		
			- $intType
			- $intCategoryId
			- $intSubId
			- $strShort
			- $strContent
			- $strImage			
			- $strLink			
			- $intParentId			
			- $strRefer			
			- $intAdminId
	 * @RESULT errCode {
				0: success
				1: not perm
				2: not perm
			}

	 */	 
	public function update($intUserId, $intShareId, $strTitle, $intType, $intCategoryId,
						   $intSubId, $strShort, $strContent, $strImage, $strLink='', 
						   $intParentId=0, $strRefer='', $intAdminId=0) {
		try {
			$this->prepare('call shares_update(?,?,?,?,?,?,?,?,?,?,?,?,?)');
			$this->bindParam(1 , $intAdminId	, PDO::PARAM_INT, 11);			
			$this->bindParam(2 , $intUserId		, PDO::PARAM_INT, 11);			
			$this->bindParam(3 , $intShareId	, PDO::PARAM_INT, 11);			
			$this->bindParam(4 , $strTitle		, PDO::PARAM_STR, 255);			
			$this->bindParam(5 , $intType		, PDO::PARAM_INT, 4);			
			$this->bindParam(6 , $intCategoryId	, PDO::PARAM_INT, 11);			
			$this->bindParam(7 , $intSubId		, PDO::PARAM_INT, 11);			
			$this->bindParam(8 , $strShort		, PDO::PARAM_STR, 255);			
			$this->bindParam(9 , $strContent	, PDO::PARAM_STR);			
			$this->bindParam(10 , $strImage		, PDO::PARAM_STR, 150);			
			$this->bindParam(11 , $strLink		, PDO::PARAM_STR, 255);			
			$this->bindParam(12 , $intParentId	, PDO::PARAM_INT, 11);			
			$this->bindParam(13 , $strRefer		, PDO::PARAM_STR);					
			$arrResult 	= $this->fetchAll();	
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : -1;
			if($intError == 0) {
				$strOldImage = isset($arrResult[0]['image']) ? $arrResult[0]['image'] : '';
				if($strOldImage != '' && $strImage != '') {
					$this->deleteImg($strOldImage);
				}
				$this->clear->deleteBoard($intCategoryId, $intSubId, $intUserId);
				$this->clear->deleteShare($intShareId);
				$objSearch		= new Ecore_Search();
				$strSlug		= Ecore_Rewrite_Ddht::slugShare($intShareId,$strTitle);
				$objSearch->addUpdate($intShareId, 2, $strTitle, '', '',
									  $strShort, $strImage, $strSlug, 0, 0,
									  Ecore_Function::now(), 1, 2, $intCategoryId, $intSubId);
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Share::update - ' . $e->getMessage());
		}
	}
	/**
	 * DELETE 
	 * @PARAM
			- $intUserId
			- $intShareId			
	 * @RESULT errCode {
				0: success
				1: not perm
			}
	 */	 
	public function delete($intUserId, $intShareId) {
		try {
			$this->prepare('call shares_udelete(?,?)');
			$this->bindParam(1 , $intUserId		, PDO::PARAM_INT, 11);			
			$this->bindParam(2 , $intShareId	, PDO::PARAM_INT, 11);	
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$intCategoryId	= isset($arrResult[0]['categoryid']) ? $arrResult[0]['categoryid'] : 0;
				$intSubId		= isset($arrResult[0]['subid']) ? $arrResult[0]['subid'] : 0;
				$intParentId	= isset($arrResult[0]['parentid']) ? $arrResult[0]['parentid'] : 0;
				$this->clear->deleteBoard($intCategoryId, $intSubId, $intUserId);
				$this->clear->deleteShare($intShareId);
				if($intParentId > 0) {
					$this->clear->deleteShare($intParentId);					
				}
				$objSearch = new Ecore_Search();
				$objSearch->updateStatus($intShareId, 2, -2);
			} 
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Share::delete - ' . $e->getMessage());
		}
	}
	/**
	 * LIKE 
	 * @PARAM
			- $intUserId
			- $intShareId			
	 * @RESULT errCode {
				0: success
				1: exists like
				2: mine's
			}
	 */	 
	public function like($intUserId, $intShareId, $intType, $intPoint) {
		try {
			$this->prepare('call shares_l_add(?,?,?,?)');
			$this->bindParam(1 , $intUserId		, PDO::PARAM_INT, 11);			
			$this->bindParam(2 , $intShareId	, PDO::PARAM_INT, 11);	
			$this->bindParam(3 , $intType		, PDO::PARAM_INT, 4);	
			$this->bindParam(4 , $intPoint		, PDO::PARAM_INT, 4);	
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$this->addShareU($intUserId, $intShareId, 2);				
				$intCategoryId	= isset($arrResult[0]['categoryid']) ? $arrResult[0]['categoryid'] : 0;
				$intSubId		= isset($arrResult[0]['subid']) ? $arrResult[0]['subid'] : 0;
				$intParentId	= isset($arrResult[0]['parentid']) ? $arrResult[0]['parentid'] : 0;
				$this->clear->deleteBoard($intCategoryId, $intSubId, $intUserId);
				$this->clear->deleteShare($intShareId);
				if($intParentId > 0) {
					$this->clear->deleteShare($intParentId);
				}
				$arrUser	= $this->getShareU($intShareId);				
				if(isset($arrUser) && $intPoint > 0) {
					$objNotification = new Notification();
					$objBehaviour	 = new Behaviour();
					$strCnt			 = Ecore_Function::wordCut($strContent);	
					foreach($arrUser as $val) {						
						if($intUserId != $val['userid']) {							
							$objNotification->add(5, $val['userid'], $intUserId, $intShareId, '');
							$objBehaviour->addUpdate($val['userid'], $intUserId, $intShareId, 0, 2, 9);
						}
					}
				}
			} 
			return $intError;
		} catch(Exception $e)  {
			Ecore_Function::logDb('Share::like - ' . $e->getMessage());
		}
	}
	private function deleteImg($strImage) {
		if(file_exists(DIR_SHARE_FULL . $strImage))
			@unlink(DIR_SHARE_FULL . $strImage);
		if(file_exists(DIR_SHARE_THUMB . $strImage))
			@unlink(DIR_SHARE_THUMB . $strImage);
		if(file_exists(DIR_SHARE_SMALL . $strImage))
			@unlink(DIR_SHARE_SMALL . $strImage);
	}
/** ----------------------------------------------- */
/** SHARE BOARD */
/** ----------------------------------------------- */
	/**
	 * CREATE BOARD 
	 * @PARAM
			- $intUserId
			- $intShareId			
	 * @RESULT 
			- INTEGER
	 */	 
	public function addBoard($intUserId, $strTitle, $strImage, $strContent) {
		try {
			$this->prepare('call shares_board_add(?,?,?,?)');
			$this->bindParam(1 , $intUserId		, PDO::PARAM_INT, 11);			
			$this->bindParam(2 , $strTitle		, PDO::PARAM_STR, 255);	
			$this->bindParam(3 , $strImage		, PDO::PARAM_STR, 150);	
			$this->bindParam(4 , $strContent	, PDO::PARAM_STR);
			$arrResult = $this->fetchAll();
			return isset($arrResult[0]['boardid']) ? $arrResult[0]['boardid'] : 0;
		} catch(Exception $e)  {
			Ecore_Function::logDb('Share::addBoard - ' . $e->getMessage());
		}
	}
	/**
	 * GET TOP 100 BOARD
	 * @PARAM
			- $intUserId
			- $intCategoryId
			- $intSubId
			- $isCombo {1:YES | 0:NO}
	 * @RESULT
			- Array | NULL
	 */
	public function getAllBoard($intUserId,$isCombo=1) {
		try {
			$strKey 	= SHARE_GETALLBOARD . $intUserId;
			$arrData	= $this->cache->get($strKey);
			if(!Ecore_Function::isCached($arrData)) {
				$this->prepare('call shares_board_getall(?)');
				$this->bindParam(1 , $intUserId		, PDO::PARAM_INT, 11);			
				$arrData 	= $this->fetchAll();
				$this->cache->set($strKey, $arrData, SHARE_GETALLBOARD_EXPIRE);
			}
			if($isCombo) {
				$arrResult	= null;
				if(isset($arrData)) {
					foreach($arrData as $value) {					
						$arrResult[$value['boardid']] = $value['title'];
					}
				}
				return $arrResult;
			} else {
				return isset($arrData) ? $arrData : null;
			}
		} catch(Exception $e) {
			Ecore_Function::logDb('Share::getAllBoard - ' . $e->getMessage());
		}
	}
/** ----------------------------------------------- */
/** SHARE PIN */
/** ----------------------------------------------- */
	/**
	 * GET BOARD INFO BY USERID & SHAREID 
	 * @PARAM
			- $intUserId
			- $intShareId			
			- $isBoardId			
	 * @RESULT 
			- INTEGER | ARRAY			
	 */	 
	public function getPin($intUserId, $intShareId, $isBoardId=0) {		
		try {
			$strKey = SHARE_GETPIN . $intUserId . $intShareId;			
			$arrResult = $this->cache->get($strKey);
			if(!Ecore_Function::isCached($arrResult)) {
				$this->prepare('call shares_pin_get(?,?)');
				$this->bindParam(1 , $intUserId		, PDO::PARAM_INT, 11);			
				$this->bindParam(2 , $intShareId	, PDO::PARAM_STR, 11);	
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, SHARE_GETPIN_EXPIRE);
			}
			if($isBoardId) {
				return isset($arrResult[0]['boardid']) ? $arrResult[0]['boardid'] : 0;
			} else {
				return isset($arrResult[0]) ? $arrResult[0] : 0;
			}
		} catch(Exception $e)  {
			Ecore_Function::logDb('Share::getPin - ' . $e->getMessage());
		}
	}
	/**
	 * ASSIGN SHARE & BOARD 
	 * @PARAM
			- $intUserId
			- $intBoardId			
			- $intShareId			
	 * @RESULT errCode {
				1: not perm
				2: exists
			}
			
	 */	 
	public function addPin($intUserId, $intBoardId, $intShareId) {
		try {
			$this->prepare('call shares_pin_add(?,?,?)');
			$this->bindParam(1 , $intUserId		, PDO::PARAM_INT, 11);			
			$this->bindParam(2 , $intBoardId	, PDO::PARAM_INT, 11);	
			$this->bindParam(3 , $intShareId	, PDO::PARAM_INT, 11);	
			$arrResult  = $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : -1;
			if($intError == 0) {
				
			} 
			return $intError;
		} catch(Exception $e)  {
			Ecore_Function::logDb('Share::addPin - ' . $e->getMessage());
		}
	}
/** ----------------------------------------------- */
/** SHARE COMMENT */
/** ----------------------------------------------- */
	public function getComment($intShareId, $intScmtId) {
		try {
			$strKey = SHARE_GETCOMMENT . $intShareId . $intScmtId;
			$arrResult	= $this->cache->get($strKey);
			if(!Ecore_Function::isCached($arrResult)) {
				$this->prepare('call shares_cmt_get(?,?)');
				$this->bindParam(1, $intShareId	, PDO::PARAM_INT, 11);			
				$this->bindParam(2, $intScmtId	, PDO::PARAM_INT, 11);			
				$arrResult 	= $this->fetchAll();	
				$this->cache->set($strKey, $arrResult, SHARE_GETCOMMENT_EXPIRE);
			}			
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Share::getComment - ' . $e->getMessage());
		}
	}
	/**
	 * GET TOTAL COMMENT
	 * @PARAM
			- $intShareId
	 * @RESULT
			- Integer
	 */
	function getTotalComment($intShareId, $intParentId) {
		return $this->getAllComment($intShareId, $intParentId);
	}
	/**
	 * GET ALL COMMENT
	 * @PARAM
			- $intShareId
			- $intParentId
			- $intOffset
			- $intLimit
			- $intTotal			: Assign value
	 * @RESULT
			- Array | NULL | Integer
	 */
	public function getAllComment($intShareId, $intParentId=0, $intOffset=0, $intLimit=CLIENT_LIMIT, &$intTotal=-1) {
		try {
			$strKey 	= '';
			$arrResult	= null;
			$intReturn	= $intTotal;
			if($intOffset < 0) $intOffset = 0;
			if($intOffset == 0) {
				$strKey 	= SHARE_GETALLCOMMENT . $intShareId . $intParentId;
				$arrResult	= $this->cache->get($strKey);
			}			
			if(!Ecore_Function::isCached($arrResult)) {				
				$this->prepare('call shares_cmt_getall(?,?,?,?)');
				$this->bindParam(1, $intShareId		, PDO::PARAM_INT, 11);			
				$this->bindParam(2, $intParentId	, PDO::PARAM_INT, 11);			
				$this->bindParam(3, $intOffset		, PDO::PARAM_INT, 11);			
				$this->bindParam(4, $intLimit		, PDO::PARAM_INT, 4);			
				$arrResult 	= $this->fetchAll();
				if($intOffset == 0) {
					$this->cache->set($strKey, $arrResult, SHARE_GETALLCOMMENT_EXPIRE);
				}
			}
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			if($intReturn < 0) {
				return $intTotal;
			}
			else
				return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Share::getAllComment - ' . $e->getMessage());
		}
	}
	/**
	 * ADD COMMENT
	 * @PARAM
			- $intUserId
			- $intShareId
			- $strContent
			- $strQuote
			- $intParentId
			- $intLevel	
	 * @RESULT errCode {
				
			}
			
	 */
	public function addComment($intUserId, $intShareId, $strContent, $strQuote='', $intParentId=0, $intLevel=1) {
		try {
			$this->prepare('call shares_cmt_add(?,?,?,?,?,?)');
			$this->bindParam(1 , $intUserId		, PDO::PARAM_INT, 11);		
			$this->bindParam(2 , $intShareId	, PDO::PARAM_INT, 11);	
			$this->bindParam(3 , $strContent	, PDO::PARAM_STR);	
			$this->bindParam(4 , $strQuote		, PDO::PARAM_STR);	
			$this->bindParam(5 , $intParentId	, PDO::PARAM_INT, 11);	
			$this->bindParam(6 , $intLevel		, PDO::PARAM_INT, 4);	
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : -1;
			if($intError == 0) {
				$intScmtId	= isset($arrResult[0]['scmtid']) ? $arrResult[0]['scmtid'] : -1;
				$this->addShareU($intUserId, $intShareId, 1);
				$arrUser	= $this->getShareU($intShareId);				
				if(isset($arrUser)) {
					$objNotification = new Notification();
					$objBehaviour	 = new Behaviour();
					$strCnt			 = Ecore_Function::wordCut($strContent);		 
					$strParams		 = Zend_Json::encode(array('id'=>$intShareId,'scmtid'=>$intScmtId,'cnt'=>$strCnt));
					$intNotiType	 = $intParentId > 0 ? 8 : 7;
					$intStype		 = $intParentId > 0 ? 11 : 10;
					foreach($arrUser as $val) {						
						if($intUserId != $val['userid']) {							
							$objNotification->add($intNotiType, $val['userid'], $intUserId, $intScmtId, $strParams);
							$objBehaviour->addUpdate($val['userid'], $intUserId, $intShareId, $intScmtId, 2, $intStype);
						}
					}
				}
				$this->clear->deleteShareComment($intShareId,$intParentId);
			}
			return $intError;
		} catch(Exception $e)  {
			Ecore_Function::logDb('Share::addComment - ' . $e->getMessage());
		}
	}
	/**
	 * ADD LIKE COMMENT
	 * @PARAM
			- $intUserId
			- $intShareId
			- $strContent
			- $strQuote
			- $intParentId
			- $intLevel	
	 * @RESULT errCode {
				1: exist
				2: mine
			}
			
	 */
	public function addLikeComment($intUserId, $intShareId, $intScmtId, $intType = 1) {
		try {
			$this->prepare('call shares_cl_add(?,?,?,?)');
			$this->bindParam(1 , $intUserId		, PDO::PARAM_INT, 11);		
			$this->bindParam(2 , $intShareId	, PDO::PARAM_INT, 11);	
			$this->bindParam(3 , $intScmtId		, PDO::PARAM_INT, 11);	
			$this->bindParam(4 , $intType		, PDO::PARAM_INT, 4);	
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : -1;
			if($intError == 0) {
				$this->addShareU($intUserId, $intShareId, 2);
				$arrUser	= $this->getShareU($intShareId);				
				if(isset($arrUser)) {
					$objNotification = new Notification();
					$objBehaviour	 = new Behaviour();
					$strCnt			 = Ecore_Function::wordCut($strContent);		 
					$strParams		 = Zend_Json::encode(array('id'=>$intShareId,'scmtid'=>$intScmtId));					
					foreach($arrUser as $val) {						
						if($intUserId != $val['userid']) {							
							$objNotification->add(9, $val['userid'], $intUserId, $intScmtId, $strParams);
							$objBehaviour->addUpdate($val['userid'], $intUserId, $intShareId, $intScmtId, 2, 12);
						}
					}
				}
				$this->clear->deleteShareComment($intShareId,$intScmtId);
			}
			return $intError;
		} catch(Exception $e)  {
			Ecore_Function::logDb('Share::addComment - ' . $e->getMessage());
		}
	}
/***************************************************/
/** SHARE USER */
/***************************************************/
	/**
	 * ADD SHARE USER
	 * @PARAM
			- $intUserId
			- $intShareId
			- $intType {0: follow; 1:cmt; 2:like}
	 */
	public function addShareU($intUserId, $intShareId, $intType) {
		try {
			$intType	= Ecore_Function::bit($intType);
			$this->prepare('call shares_u_add(?,?,?)');
			$this->bindParam(1, $intUserId	, PDO::PARAM_INT, 11);			
			$this->bindParam(2, $intShareId	, PDO::PARAM_INT, 11);			
			$this->bindParam(3, $intType	, PDO::PARAM_INT, 6);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : -1;
			if($intError == 0) {
				$this->clear->deleteShareU($intShareId, $intType);
			}
		} catch(Exception $e) {
			Ecore_Function::logDb('Share::addShareU - ' . $e->getMessage());
			
		}
	}
	/**
	 * GET SHARE USER
	 * @PARAM			
			- $intShareId
			- $intType
	 */
	public function getShareU($intShareId, $intType=-1) {
		try {
			if($intType >= 0) $intType = Ecore_Function::bit($intType);			
			$strKey		= SHARE_GETSHAREU . $intShareId . $intType;
			$arrResult	= $this->cache->get($strKey);
			if(!Ecore_Function::isCached($arrResult)) {
				$this->prepare('call shares_u_getall(?,?)');			
				$this->bindParam(1, $intShareId	, PDO::PARAM_INT, 11);			
				$this->bindParam(2, $intType	, PDO::PARAM_INT, 6);			
				$arrResult	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, SHARE_GETSHAREU_EXPIRE);
			}
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Share::getShareU - ' . $e->getMessage());
			
		}
	}
}